﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography;
using System.Web.Security;
using System.Text;
using LogicLevel;

namespace WebLevel
{
    public partial class Homepage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                //Label1.Text = Session["userID"].ToString();
                int userID = Convert.ToInt32(Session["userID"]);
                String nickName = "", email = "", imageUrl = "", major = "", signature = "";
                short enteryear = 0;
                int valid = UserMethod.getUserPersonalInfo(userID, ref nickName, ref email, ref imageUrl, ref major, ref enteryear, ref signature);
                if (valid != -1)
                {
                    Label_Email.Text = email;
                    TextBox_Nickname.Text = nickName;
                    TextBox_Major.Text = major;
                    TextBox_Grade.Text = enteryear.ToString();
                    TextBox_Signature.Text = signature;
                    CancelEditButton.Visible = false;
                }
            }
        }

        protected void EditButton_Click(object sender, EventArgs e)
        {
            if (TextBox_Nickname.ReadOnly == true)
            {
                TextBox_Nickname.ReadOnly = false;
                TextBox_Major.ReadOnly = false;
                TextBox_Grade.ReadOnly = false;
                TextBox_Signature.ReadOnly = false;
                CancelEditButton.Visible = true;

                TextBox_Nickname.BorderWidth = 1;
                TextBox_Major.BorderWidth = 1;
                TextBox_Grade.BorderWidth = 1;
                TextBox_Signature.BorderWidth = 1;
            }
            else
            {
                int userID = Convert.ToInt32(Session["userID"]);
                UserMethod.setUserPersonalInfo(userID, TextBox_Nickname.Text, TextBox_Major.Text,
                    Convert.ToInt16(TextBox_Grade.Text), TextBox_Signature.Text);
                CancelEditButton.Visible = false;
                TextBox_Nickname.ReadOnly = true;
                TextBox_Major.ReadOnly = true;
                TextBox_Grade.ReadOnly = true;
                TextBox_Signature.ReadOnly = true;
                TextBox_Nickname.BorderWidth = 0;
                TextBox_Major.BorderWidth = 0;
                TextBox_Grade.BorderWidth = 0;
                TextBox_Signature.BorderWidth = 0;
            }
        }
        protected void CancelEditButton_Click(object sender, EventArgs e)
        {
            CancelEditButton.Visible = false;
            TextBox_Nickname.ReadOnly = true;
            TextBox_Major.ReadOnly = true;
            TextBox_Grade.ReadOnly = true;
            TextBox_Signature.ReadOnly = true;
            TextBox_Nickname.BorderWidth = 0;
            TextBox_Major.BorderWidth = 0;
            TextBox_Grade.BorderWidth = 0;
            TextBox_Signature.BorderWidth = 0;
        }

        protected void Button_ChangePwd_Click(object sender, EventArgs e)
        {
            if (TextBox_Newpwd.Text.Equals(TextBox_ConfirmNewpwd.Text))
            {
                int userID = Convert.ToInt32(Session["userID"]);
                 MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
                string oldpwd = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(TextBox_Oldpwd.Text)), 4, 8).ToLower().Replace("-", "");
                string newpwd = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(TextBox_Newpwd.Text)), 4, 8).ToLower().Replace("-", "");
             
                UserMethod.changePwd(userID, oldpwd, newpwd);
            }
        }

        protected void Button_CancelChangePwd_Click(object sender, EventArgs e)
        {

        }
    }
}